package com.simtop.controller;

import com.alibaba.fastjson.JSONObject;
import com.simtop.annotation.RepeatSubmit;
import com.simtop.aop.SystemControllerLog;
import com.simtop.domain.experiment.ExperimentProject;
import com.simtop.domain.experiment.ExperimentProjectVo;
import com.simtop.service.ExperimentProjectService;
import com.simtop.utils.ResultBean;
import com.simtop.utils.convert.Convert;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.validation.Valid;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;
import java.util.Arrays;
import java.util.List;

/**
 * @author lvyongwei
 */
@RequestMapping("/experimentProject")
@RestController
public class ExperimentProjectController extends BaseController{

    @Autowired
    private ExperimentProjectService experimentProjectService;

    /**
     * 添加虚拟仿真教育项目
     * @param experimentProject
     * @param photo
     * @param video
     * @return
     */
    @SystemControllerLog(description = "添加高校虚拟仿真建设内容")
    @PostMapping("/add")
    @RepeatSubmit
    public ResultBean insertExperimentProject(@Valid ExperimentProject experimentProject,
                                              @NotNull(message = "封面图片必须上传") MultipartFile photo,
                                              @NotNull(message = "项目简介视频必须上传") MultipartFile video){
        return ResultBean.toResult(experimentProjectService.insertExperimentProject(experimentProject,photo,video));
    }
    /**
     * 修改信息
     * @param experimentProject
     * @param photo
     * @param video
     * @return
     */
    @SystemControllerLog(description = "修改高校虚拟仿真建设内容")
    @PostMapping("/update")
    @RepeatSubmit
    public ResultBean updateExperimentProject(ExperimentProject experimentProject, MultipartFile photo, MultipartFile video){
        return ResultBean.toResult(experimentProjectService.updateExperimentProject(experimentProject,photo,video));
    }

    /**
     * 查询列表
     * @param experimentProject
     * @return
     */
    @PostMapping("/getAll")
    public ResultBean getAllExperimentProject(@RequestBody ExperimentProject experimentProject){
        if(experimentProject.getIfPage() != null && experimentProject.getIfPage() == false){
            return ResultBean.success(experimentProjectService.getExperimentProject(experimentProject));
        }
        startPage(experimentProject);
        List<ExperimentProjectVo> experimentProjectList = experimentProjectService.getExperimentProject(experimentProject);
        return ResultBean.success("查询成功",getDataTable(experimentProjectList));
    }

    /**
     * 删除信息
     * @param data
     * @return
     */
    @SystemControllerLog(description = "删除高校虚拟仿真建设内容")
    @PostMapping("/delete")
    public ResultBean deleteExperimentProject(@RequestBody @NotBlank(message = "参数不允许为空") String data){
        /**
         * 获取参数
         */
        JSONObject jsonObject = JSONObject.parseObject(data);
        String ids = jsonObject.getString("experimentIds");
        List<Integer> args = Arrays.asList(Convert.toIntArray(ids));
        return ResultBean.toResult(experimentProjectService.deleteExperimentProject(args));
    }

    /**
     * 获取详细信息
     * @param data
     * @return
     */
    @PostMapping("/getById")
    public ResultBean getExperimentProjectById(@RequestBody @NotBlank(message = "参数不允许为空") String data){
        JSONObject jsonObject = JSONObject.parseObject(data);
        Integer id = jsonObject.getInteger("experimentId");
        return ResultBean.success(experimentProjectService.getExperimentProjectById(id));
    }
}